package com.model;

import com.model.factory.AbstractFactory;
import com.model.factory.FactoryProducer;
import com.model.factory.Frame;
import com.model.factory.SimpleFactory;
import com.model.prototype.Shape;
import com.model.prototype.ShapeCache;
import com.model.singleCase.HungrySingle;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.stream.Collectors.toList;

/**
 * @author: cxt
 * @time: 2022/2/18
 * 测试设计模式类
 */
public class TestModel {
    // 单例
    @Test
    public void test() {
        // SingleCase singleCase = new SingleCase();  单例类的构造方法是私有的，不能这样创建对象
        // 获取单例对象方法(饿汉式单例)
        HungrySingle instance = HungrySingle.getInstance();
        instance.test();
    }

    // 原型模式
    @Test
    public void test1() {
        // 初始化，使得对象都有实例，后面才可以进行克隆
        ShapeCache.queryData();

        Shape clonedShape = ShapeCache.getShape("1001");
        clonedShape.draw();

        Shape clonedShape2 = ShapeCache.getShape("1002");
        clonedShape2.draw();
    }

    // 工厂模式
    @Test
    public void test2() {
        Frame frame = SimpleFactory.getShape("RECTANGLE");
        frame.draw();
    }

    // 抽象工厂模式
    @Test
    public void test3() {
        // 生成工厂
        AbstractFactory frame = FactoryProducer.getFactory("FRAME");

        // 获取具体工厂得到对象
        Frame rectangle = frame.getShape("RECTANGLE");
        // 执行具体方法
        rectangle.draw();
    }

    @Test
    public void test4() {
        List<String> list = new ArrayList<String>();
        list.add("I am a boy");
        list.add("I love the girl");
        list.add("But the girl loves another girl");

        List<String> collect = list.stream().map(line -> line.split(" ")) // 将每个字符串按空格分隔开
                .flatMap(Arrays::stream) // 将小流合并成一个大流
                .distinct() // 去重
                .collect(toList());

        System.out.println(collect);
    }

    @Test
    public void tes() {
        ArrayList<String> list = new ArrayList<>();
        list.add("cxt");
        list.add("cdasa");

        for (String s : list) {
            if ("cdasa".equals(s)) {
                list.remove(s);
                System.out.println("list-->" + list.size());
            }

            ArrayList<String> strings = new ArrayList<>();
            strings.add("test");
            strings.add("string");

            strings.removeIf("string"::equals);
            System.out.println("string-->" + list.size());
        }
    }

    @Test
    public void test11() {
        //连接redis
        Jedis jedis = new Jedis("192.168.124.4", 6379);
        //查看服务器是否运行，打出 pong 表示OK
        System.out.println("ping redis：" + jedis.ping());
    }

}
